using System;
using System.Web;
using System.Web.UI.HtmlControls;
using DotNetNuke.Data;
using ITCrossing.Common;

namespace ITCrossing
{
	public partial class metaPostRedirect : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

			string intendedUrl = string.Empty;
			Guid urlKeyGUID;
			string providerKey = string.Empty;
			string blogredirect = string.Empty;

			// Retrieve the IntendedUrl from the QueryString
			if (Request["urlkey"] != null && Request["urlkey"].ToString() != string.Empty)
			{
				urlKeyGUID = new Guid(Request["urlkey"].ToString());

				// Retreive the intended URL from the urlkey
				string sSQL = "itcMetaPost_Get_Blog_Redirect_URL";

				object[] methodParams = { urlKeyGUID };

				// Get the URL Key for the Redirect URL.  This sproc will create the URL key if it doesn't exist.
				intendedUrl = DataProvider.Instance().ExecuteScalar(sSQL, methodParams).ToString();

			}

			// Retrieve the providerKey from the QueryString
			if (Request["key"] != null)
			{
				providerKey = HttpUtility.UrlDecode(Request["key"].ToString());
			}

			// Check to see if we should redirect
			if (Request["blogredirect"] != null)
			{
				blogredirect = HttpUtility.UrlDecode(Request["blogredirect"].ToString());
			}

			if (!string.IsNullOrEmpty(intendedUrl) && !string.IsNullOrEmpty(providerKey))
			{
				// Redirect to the intended URL using client side code
				//string rs = "<script type=\"text/javascript\">";
				//rs += "window.location.href = '" + Request["IntendedUrl"].ToString() + "';";
				//rs += "</script>";

				if (!string.IsNullOrEmpty(blogredirect))
				{
					Response.Redirect(intendedUrl);
				}

                scriptRedirect.Text = "<script type=\"text/javascript\">window.location.href = '" + intendedUrl + "';</script>";
                // Place some JavaScript into the page to redirect to the intendedUrl in case the redirect doesn't
                // occur server side.  This happens when the View Blog link is clicked in WLW.

                Page.ClientScript.RegisterClientScriptBlock(typeof(metaPostRedirect), "redirectScript", "window.location.href = '" + intendedUrl + "';");

				//if (Request.UserAgent.IndexOf("Windows Live Writer") < 0)
				//{
				//    //HtmlGenericControl script = new HtmlGenericControl("script");
				//    //script.Attributes.Add("type", "text/javascript");
				//    //script.InnerText = "window.location.href = '" + HttpUtility.UrlDecode(intendedUrl) + "';";
				//    //pageBody.Controls.Add(script);
				//    Response.Redirect(intendedUrl);
				//}

				ProviderStruct providerInfo = CommonServices.GetProviderInfo(providerKey);

				HtmlGenericControl link = new HtmlGenericControl("link");
				link.Attributes.Add("rel", "wlwmanifest");
				link.Attributes.Add("type", "application/wlwmanifest+xml");
				link.Attributes.Add("href", DotNetNuke.Common.Globals.ApplicationPath + providerInfo.ManifestPath);
				link.Visible = true;
				head.Controls.Add(link);

			}
		}
	}
}
